package com.ant.cn.views;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by baichuan on 2016/12/3.
 * 测试
 */

public class MyView001 extends View {
    public MyView001(Context context) {
        super(context);
        initThisView(context, null, 0);
    }

    public MyView001(Context context, AttributeSet attrs) {
        super(context, attrs);
        initThisView(context, attrs, 0);
    }

    public MyView001(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initThisView(context, attrs, defStyleAttr);
    }

    private void initThisView(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mesureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int model=MeasureSpec.getMode(heightMeasureSpec);
        int size=MeasureSpec.getSize(heightMeasureSpec);

        if(model==MeasureSpec.EXACTLY){
            result=size;
        }else{
            result=10;
            if(model==MeasureSpec.AT_MOST)
                result=Math.min(result,size);
        }

        return result;
    }

    private int mesureWidth(int widthMeasureSpec) {
        int result = 10;
        int model=MeasureSpec.getMode(widthMeasureSpec);
        int size=MeasureSpec.getSize(widthMeasureSpec);

        if(model==MeasureSpec.EXACTLY){
            result=size;
        }else{
            result=10;
            if(model==MeasureSpec.AT_MOST)
                result=Math.min(result,size);
        }


        return result;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        return super.onTouchEvent(event);
    }
}
